<!DOCTYPE html>
<html>
<head>
<script>
function log(message)
{
    document.getElementById('console').appendChild(document.createTextNode(message + "\n"));
}

let kHeapSize = 4096;

function runTest()
{
    log("Test calling asm functions across workers with the same heap.");
    var string = [
        "worker = this;",
        "onmessage = function(e) {",
        "    function Module(stdlib, foreign, heap) {",
        "        'use asm';",
        "        var MEM32 = new stdlib.Int32Array(heap);",
        "        function load() { var res = 0; res = MEM32[1023]|0; MEM32[1023] = 123457; return res|0; }",
        "        return { load: load };",
        "    }",
        "    var buffer = e.data;",
        "    var result = Module(worker, {}, buffer).load();",
        "    var message = result == 123456 ? 'SUCCESS' : 'FAILURE ' + result;",
        "    postMessage(message);",
        "};"
    ].join('\n');
    var blobURL = URL.createObjectURL(new Blob([string]));
    var worker = new Worker(blobURL);
    function Module(stdlib, foreign, heap) {
        'use asm';
        var MEM32 = new stdlib.Int32Array(heap);
        function store() { MEM32[1023] = 123456; }
        return { store: store };
    }
    var buffer = new ArrayBuffer(kHeapSize);
    Module(this, {}, buffer).store();
    worker.onmessage = function(event) {
        try {
            // This buffer may or may not have been neutered, ensure we don't write across threads.
            for (var i = 0; i < kHeapSize; i++) {
                buffer[i] = 0;
            }
        } catch(e) {}
        log(event.data);
        log("DONE");
        if (testRunner.notifyDone)
            testRunner.notifyDone();
    };
    worker.postMessage(buffer);
}

if (window.testRunner) {
    testRunner.setAllowFileAccessFromFileURLs(true);
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}
</script>
</head>
<body onload="runTest()">
<pre id='console'></pre>
</body>
</html>
